<?php
namespace app\queen\controller;
use app\common\model\Redis;
use app\common\model\Dev_info;
use think\Controller;
class Index extends Controller {
    public $post;
    public $redis;
    
    public function __construct(){
        
        parent::__construct();

        $this->post = input('post.');

        $this->redis = new Redis();
    }
    
    /**
     * 判断设备是否激活
     */
    public function ifActivate(){
        $dev_id = $this->post["dev_id"];
        $model = new Dev_info();
        $row = $model->getOne(['guid1'=>$dev_id],'state');

        if($row["state"] == "inactive"){
            $res = "NO";
        }elseif($row == ""){
            $res = "NU";
        }else{
            $res = "OK";
        }

        exit($res);
    }

    /**
     * 加载所有需要在线告知的设备至redis中的程序
     */
    public function informDev(){
        $this->redis->select(2);
        $devArr = $this->redis->keys("*");
        $time = time();
        
        foreach ($devArr as $key => $devid) {
        
            //(1)轮询设备进行警报处理.对于单台设备,警报处理流程为
            $alarm_temp_valid = $this->redis->hget($devid,"alarm_temp_valid");
            $alarm_valid = $this->redis->hget($devid,"alarm_valid");

            if($alarm_temp_valid=="N" || $alarm_valid=="N"){
                continue;
            }

            $alarm_has_send_num = $this->redis->hget($devid,"alarm_has_send_num");
            $alarm_send_num = $this->redis->hget($devid,"alarm_send_num");

            if($alarm_has_send_num>=$alarm_send_num){
               $this->redis->hset($devid,"temp_valid","N");
               continue;
            }

            $last_alarm_inform_t = $this->redis->hget($devid,"last_alarm_inform_t");
            $intv = $this->redis->hget($devid,"intv");
            $ts = ($time-$last_alarm_inform_t)/60;
            if($ts<$intv){
                continue;
            }

            //更新last_alarm_inform_t,alarm_has_send_num自增1
            $last_alarm_inform_t = $this->redis->hget($devid,"last_alarm_inform_t");
            $alarm_has_send_num = $this->redis->hget($devid,"alarm_has_send_num");

            $this->redis->hset($devid,"last_alarm_inform_t",time());
            $this->redis->hset($devid,"alarm_has_send_num",$alarm_has_send_num+1);
            

            //(2)轮询需要在线告知的设备，对单台设备的在线告知处理为：
            //当前时间戳与last_online_inform_t间的时间差,如果小于5.5分钟,执行掉线记录复原,并退出
            
            $last_online_inform_t = $this->redis->hget($devid,"last_online_inform_t");
            $ctime = 5*60+30;
            if($last_online_inform_t<$ctime){
                continue;
            }

            $online_inform_temp_valid = $this->redis->hget($devid,"online_inform_temp_valid");
            $online_inform_valid = $this->redis->hget($devid,"online_inform_valid");

            if($online_inform_temp_valid=="N" || $online_inform_valid=="N"){
                continue;
            }

            $last_online_send_t = $this->redis->hget($devid,"last_online_send_t");
            $online_inform_intv = $this->redis->hget($devid,"online_inform_intv");
            if($time-$last_online_send_t < $online_inform_intv){
                continue;
            }

            $online_inform_has_send_num = $this->redis->hget($devid,"online_inform_has_send_num");
            $online_inform_send_num = $this->redis->hget($devid,"online_inform_send_num");
            if($online_inform_has_send_num >= $online_inform_send_num){
                $this->redis->hset($devid,"online_inform_temp_valid","N");
                continue;
            }

            //更新last_online_send_t,online_inform_has_send_num自增1
            $this->redis->hset($devid,"last_online_send_t",$last_online_send_t+1);
            $this->redis->hset($devid,"online_inform_has_send_num",$online_inform_has_send_num+1);

            if($online_inform_has_send_num >= $online_inform_send_num){
                $this->redis->hset($devid,"online_inform_temp_valid","N");
                continue;
            }
        }
    }
    
}